package cn.jhc.teachingquality.activity;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import org.junit.Rule;
import org.junit.runner.RunWith;

import cn.jhc.teachingquality.R;
import cn.jhc.teachingquality.adapter.TeachingPagerAdapter;
import cn.jhc.teachingquality.fragment.PracticeLogListFragment;
import cn.jhc.teachingquality.fragment.TeachingLogListFragment;

import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

/**
 * Created by luyanfei on 16-5-31.
 */
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> activityRule =
            new ActivityTestRule<MainActivity>(MainActivity.class);

    private MainActivity mainActivity;

    @org.junit.Before
    public void setUp() throws Exception {
        mainActivity = activityRule.getActivity();

    }

    @org.junit.After
    public void tearDown() throws Exception {

    }

    @org.junit.Test
    public void testOnCreate() throws Exception {
        Fragment mainFragment = mainActivity.getSupportFragmentManager().findFragmentById(R.id.main_container);
        assertNotNull(mainFragment);
        ViewPager viewPager = (ViewPager)mainFragment.getView().findViewById(R.id.main_content);
        assertNotNull(viewPager);
        PagerAdapter adapter = viewPager.getAdapter();
        assertThat(adapter, instanceOf(TeachingPagerAdapter.class));
        TeachingPagerAdapter teachingPagerAdapter = (TeachingPagerAdapter)adapter;
        Fragment fragment0 = teachingPagerAdapter.getItem(0);
        assertThat(fragment0, instanceOf(TeachingLogListFragment.class));
        Fragment fragment1 = teachingPagerAdapter.getItem(1);
        assertThat(fragment1, instanceOf(PracticeLogListFragment.class));
    }
}